package com.oauth2.web;

import com.oauth2.domain.UserProfile;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author shipeilei
 * @date 2020-7-16 10:49:57
 * @desc desc
 */
@Controller
public class UserController {
    @RequestMapping("/api/profile")
    public ResponseEntity<UserProfile> profile() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserProfile profile = new UserProfile();
        profile.setUsername(user.getUsername());
        for (GrantedAuthority authority : user.getAuthorities()) {
            profile.getAuthorities().add(authority.getAuthority());
        }
        return ResponseEntity.ok(profile);
    }
}
